// index.js
import * as request from "../../utils/request/user/user"
import Notify from "@vant/weapp/notify/notify.js"

console.log(Notify)
Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      userAccount: '',
      userPassword: ''
    },

  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad(options) {
    
    wx.getStorage({
      key: 'userInfo',
      success(res) {
        console.log(res)
        if (res.data != null) {
          Notify({
            message: res.data.message,
            duration: 800,
            selector: '#custom-selector',
            // background: '#00ffff',
            type: "success",
            color: "#ffff",
          });
          setTimeout(() => {
            wx.switchTab({
              url: '../home/index',
              fail(err) {
                console.log(err)
              }
            })
          }, 800)
        }
      },
    })
   
  },
  onInputChange(e) {

    const info = e.detail.value
    if (e.target.id === '0') {
      this.setData({
        "userInfo.userAccount": info,
      })
    } 
    if (e.target.id === '1'){
      this.setData({
        "userInfo.userPassword": info,
      })
    }

  },
  async handleLogin() {
      let data = this.data.userInfo;
      //console.log(data)
     if(!data.userPassword||!data.userAccount){
      Notify({
        message: "账号或密码不能为空",
        duration: 1000,
        selector: '#custom-selector',
        // background: '#00ffff',
        type: "danger",
        color: "#ffff",
      });
     }
      let res = await request.login(data)
     
      
      if (res.data.code === 0) {
        // console.log("heard",res.header['Set-Cookie'])
        Notify({
          message: res.data.message,
          duration: 1000,
          selector: '#custom-selector',
          // background: '#00ffff',
          type: "success",
          color: "#ffff",
        });
        wx.setStorage({
          key: "userInfo",
          data: res.data,
          success() {
          }
        })
        wx.setStorage({
          key: "session",
          data: res.header['Set-Cookie'],
          success() {
            console.log(wx.getStorageSync('session'))
          }
        })
        setTimeout(() => {
          wx.switchTab({
            url: '../home/index',
            fail(err) {
              console.log(err)
            }
          })
        }, 1000)
      }else{
        console.log(333)
        Notify({
          message: res.data.message,
          duration: 1000,
          selector: '#custom-selector',
          // background: '#00ffff',
          type: "danger",
          color: "#ffff",
        });
      }
  
  }
})